local skel = fk.CreateSkill {
  name = "emo__xiucheng",
}

Fk:loadTranslationTable{
  ["emo__xiucheng"] = "朽城",
  [":emo__xiucheng"] = "准备阶段，你可以视为使用【酒】【决斗】【过河拆桥】中的两张，每张结算后目标摸一张牌，其可令之视为【无懈可击】或【闪】。",
  -- 酒计入次数限制
  ["#emo__xiucheng-filter"] = "朽城：你可以令%arg视为【无懈可击】或【闪】",
  ["@@emo__xiucheng-inhand"] = "朽城",
  ["#emo__xiucheng-use"] = "朽城:你可以视为使用【酒】、【决斗】或【过河拆桥】，再令目标摸1牌",
  ["#emo__xiucheng-second"] = "朽城:再视为使用另一种牌！",
}

---@param use UseCardDataSpec
local drawTarget = function(use)
  if not (use.tos and #use.tos > 0) then return end
  for _, to in ipairs(use.tos) do
    if not to.dead then
      local cid = to:drawCards(1, skel.name)[1]
      if cid and table.contains(to:getCardIds("h"), cid) then
        local choice = to.room:askToChoice(to, {
          skill_name = skel.name, choices = {"nullification", "jink", "Cancel"},
          prompt = "#emo__xiucheng-filter:::"..Fk:getCardById(cid):toLogString()
        })
        if choice ~= "Cancel" then
          to.room:setCardMark(Fk:getCardById(cid), "@@emo__xiucheng-inhand", choice)
          Fk:filterCard(cid, to)
        end
      end
    end
  end
end


skel:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Start
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local names = {"analeptic", "duel", "dismantlement"}
    local use = room:askToUseVirtualCard(player, {
      name = names, skill_name = skel.name, prompt = "#emo__xiucheng-use", skip = true, extra_data = {bypass_times = false},
    })
    if use then
      use.extraUse = false
      event:setCostData(self, {use = use})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local names = {"analeptic", "duel", "dismantlement"}
    local use = event:getCostData(self).use
    table.removeOne(names, use.card.name)
    room:useCard(use)
    drawTarget(use)
    if player.dead then return end
    use = room:askToUseVirtualCard(player, {
      name = names, skill_name = skel.name, prompt = "#emo__xiucheng-second", skip = true, extra_data = {bypass_times = false},
      cancelable = false,
    })
    if use then
      use.extraUse = false
      room:useCard(use)
      drawTarget(use)
    end
  end,
})

skel:addEffect("filter", {
  card_filter = function(self, card, player)
    return card:getMark("@@emo__xiucheng-inhand") ~= 0
  end,
  view_as = function(self, player, card)
    local c = Fk:cloneCard(card:getMark("@@emo__xiucheng-inhand"), card.suit, card.number)
    c.skillName = skel.name
    return c
  end,
})

return skel
